home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / arts / pool.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-09-10  |  2.3 KB  |  90 lines

  1.     /*
  2.  
  3.     Copyright (C) 2000 Stefan Westerfeld
  4.                        stefan@space.twc.de
  5.  
  6.     This library is free software; you can redistribute it and/or
  7.     modify it under the terms of the GNU Library General Public
  8.     License as published by the Free Software Foundation; either
  9.     version 2 of the License, or (at your option) any later version.
  10.   
  11.     This library is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.     Library General Public License for more details.
  15.    
  16.     You should have received a copy of the GNU Library General Public License
  17.     along with this library; see the file COPYING.LIB.  If not, write to
  18.     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  19.     Boston, MA 02111-1307, USA.
  20.  
  21.     */
  22.  
  23. #ifndef ARTS_POOL_H
  24. #define ARTS_POOL_H
  25.  
  26.  
  27. /*
  28.  * BC - Status (2002-03-08): Pool<type>
  29.  *
  30.  * Needs to be kept binary compatible by NOT TOUCHING. When you want something
  31.  * else, write a fresh one (used as part of Arts::Dispatcher, thus changing
  32.  * this breaks Arts::Dispatcher binary compatibility).
  33.  */
  34.  
  35.  
  36. /**
  37.  * A pool object of the type T keeps a pool of T* pointers, that are numbered.
  38.  *
  39.  * You allocate and release slots, and store T*'s in there. It should take
  40.  * about no time to find a new free slot to store the T object into and to
  41.  * release a slot to be reused.
  42.  *
  43.  * The pool object internally keeps track which slots are used.
  44.  */
  45. #include <stack>
  46. #include <vector>
  47. #include <list>
  48.  
  49. namespace Arts {
  50.  
  51. template <class T>
  52. class Pool {
  53.     std::stack<unsigned long> freeIDs;
  54.     std::vector<T *> storage;
  55. public:
  56.     inline T*& operator[](unsigned long n) { return storage[n]; }
  57.     inline void releaseSlot(unsigned long n) {
  58.         freeIDs.push(n);
  59.         storage[n] = 0;
  60.     }
  61.     unsigned long allocSlot() {
  62.         unsigned long slot;
  63.         if(freeIDs.empty())
  64.         {
  65.             unsigned long n;
  66.             for(n=0;n<32;n++) {
  67.                 freeIDs.push(storage.size());
  68.                 storage.push_back(0);
  69.             }
  70.         }
  71.         slot = freeIDs.top();
  72.         freeIDs.pop();
  73.         return slot;
  74.     }
  75.     std::list<T *> enumerate() {
  76.         std::list<T *> items;
  77.         //std::vector<T *>::iterator i;
  78.         int n,max = storage.size();
  79.  
  80.         for(n=0; n < max; n++)
  81.             if(storage[n]) items.push_back(storage[n]);
  82.  
  83.         return items;
  84.     }
  85.     unsigned long max() { return storage.size(); }
  86. };
  87.  
  88. }
  89. #endif /* POOL_H */
  90.